<?php
// $Id$

/**
 * Controller_Spider 控制器
 */
class Controller_Spider extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
        ini_set("max_execution_time",30);

        try{
            $code = 600000;
            $entfile = "/var/www/spider/60ent.txt";
            $secfile = "/var/www/spider/60sec.txt";
            $mkt_id = 21;
            $type = 'stock';
            $i = 0;
            $page = "";
            $entfp = fopen($entfile, "w+");
            $secfp = fopen($secfile, "w+");
            for(; $i < 2; $i++){
                $code = $code + $i;
                $errno = 0;
                $errstr = "";
                $fp = fsockopen("quote.eastmoney.com", 80, &$errno, &$errstr, 100);
                if (!$fp){
                    $page = "$errstr ($errno) <br />\n";
                    return $page;
                }else{
                    $header = "GET /$code.html HTTP/1.0\n"
                    ."Host: quote.eastmoney.com\n"
                    ."Accept-Language:en-us,en;q=0.5\n"
                    ."Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
                    ;
                    fputs($fp, $header);
                    $content = "";
                    $page .="$header<br />";
                    $page .="content: $content";
                    while(!feof($fp)){
                        $content .= fgets($fp, 1024);
                    }
                    fclose($fp);
                    $regs = array();
                    ereg('公司名称｜([^&｜]+)', $content, $regs);
                    
                    if(isset($regs[0])){
                        $ent = trim($regs[0]);
                    }else{
                        $ent = "";
                    }
                    ereg('证券简称｜([^&｜]+).*证券代码｜([^&｜]+)',$content, $regs);
                    if (count($regs) == 2){
                        if ($code != trim($regs[2]))
                            $page .="$code: error, 证券代码不等于$code<br />\n";
                        }else{
                            $name = trim($regs[1]);
                    }

                    ereg('联系电话｜([^&｜]+)', $content, $regs);
                    if (!empty($regs)){
                        $tel = trim($regs[1]);
                    }else{
                        $tel = "";
                    }

                    ereg("传[&nbsp;]*真([^&｜]+)", $content, $regs);
                    if(!empty($regs)){
                        $fax = trim($regs[1]);
                    }else{
                        $fax = "";
                    }
                    $addr = $email = $cate = $site = $intro = "";
                    $page .= "$code: {$name}, ent: {$ent}<br />\n";
                    fwrite($secfp, "$mkt_id\t$code\t$name\t$type\t$ent\n");
                    fwrite($entfp, "$ent\t$addr\t$tel\t$fax\t$email\t$cate\t$site\t$intro\n");
                
                }
            }
            fclose($secfp);
            fclose($entfp);
        }catch(Exception $e){
            $page .= $e."<br />\n"; 
        }

        return $page;
	}
}


